package com.xstech.library.rxjavahttp;

import android.content.pm.PackageManager;
import android.util.Log;

import com.blankj.utilcode.util.DeviceUtils;
import com.xstech.library.app.BaseApp;
import com.xstech.library.utils.StringUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderInterceptor implements Interceptor {


    public static String X_Token = "";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        String User_Agent = getAgent();
        Request.Builder requestBuilder = originalRequest.newBuilder()
                .addHeader("User-Agent", User_Agent)
                .method(originalRequest.method(), originalRequest.body());
        if (!StringUtils.isEmpty(X_Token)) {
            requestBuilder.addHeader("X-Token", X_Token);
        }
        Request request = requestBuilder.build();
        return chain.proceed(request);
    }

    private String getAgent() {
        String timeString = "";
        String version = "";
        try {
            version = BaseApp.getInstance().getPackageManager().getPackageInfo(BaseApp.getInstance().getPackageName(), 0).versionName + "";
            timeString = BaseApp.getInstance().getPackageManager().getPackageInfo(BaseApp.getInstance().getPackageName(), 0).versionCode + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String model = DeviceUtils.getModel();
        String manufacturer = DeviceUtils.getManufacturer();
        String user_Agent = "com.xingshentech.ugv.operate__" + version + "." + timeString + "__" +
                BaseApp.getApp().getNetworkType() + "__" + BaseApp.getApp().getScreen() + "__" + model + "__" + manufacturer;
        return user_Agent;
    }
}
